﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Caching;
using System.Web.Mvc;
using ProjetoSolo.QueroGas.Aplicacao.Servicos;
using ProjetoSolo.QueroGas.Dominio.Entidades;

namespace Gas.Controllers
{
    public class HomeController : Controller
    {
        //
        // GET: /Home/

        public ActionResult Index()
        {
            return View();
        }

        public ActionResult Login()
        {
            return View();
        }

        // GET: /Home/Details/5

        public ActionResult Details(int id)
        {
            return View();
        }

        //
        // GET: /Home/Create

        public ActionResult Create()
        {
            return View();
        }

        //
        // POST: /Home/Create

        [HttpPost]
        public ActionResult Create(FormCollection collection)
        {
            try
            {
                // TODO: Add insert logic here

                return RedirectToAction("Index");
            }
            catch
            {
                return View();
            }
        }

        //
        // GET: /Home/Edit/5

        public ActionResult Edit(int id)
        {
            return View();
        }

        //
        // POST: /Home/Edit/5

        [HttpPost]
        public ActionResult Edit(int id, FormCollection collection)
        {
            try
            {
                // TODO: Add update logic here

                return RedirectToAction("Index");
            }
            catch
            {
                return View();
            }
        }

        //
        // GET: /Home/Delete/5

        public ActionResult Delete(int id)
        {
            return View();
        }

        //
        // POST: /Home/Delete/5

        [HttpPost]
        public ActionResult Delete(int id, FormCollection collection)
        {
            try
            {
                // TODO: Add delete logic here

                return RedirectToAction("Index");
            }
            catch
            {
                return View();
            }
        }
        public ActionResult FiltrarEnderecos(string parametro)
        {
            IEnumerable<Endereco> enderecos = null;
            var obj = System.Web.HttpContext.Current.Cache.Get("enderecos");
            if (obj == null)
            {
                enderecos = ServicoEndereco.BuscarBairrosECidades();
                System.Web.HttpContext.Current.Cache.Add("enderecos", enderecos, null, DateTime.Now.AddHours(2), Cache.NoSlidingExpiration, CacheItemPriority.Normal, null);
            }
            else
                enderecos = (IList<Endereco>)obj;
            
            IList<Endereco> enderecosFiltrados = (from endereco in enderecos where 
                                                  RemoverAcentosString(endereco.Cidade).ToLower().StartsWith(parametro.ToLower()) || 
                                                  RemoverAcentosString(endereco.Bairro).ToLower().StartsWith(parametro.ToLower()) select endereco).Distinct().ToList();

            var enderecosJson = enderecosFiltrados.Select(endereco => new
            {
                bairro = endereco.Bairro,
                cidade = endereco.Cidade,
                categoria = String.IsNullOrEmpty(endereco.Bairro) ? "Cidade" : "Bairro"
            });

            return Json(enderecosJson);
        }

        // TODO: Mover esse metodo para um lugar mais adequado.
        private static string RemoverAcentosString(string str)
        {
            var acentos = new string[] { "ç", "Ç", "á", "é", "í", "ó", "ú", "ý", "Á", "É", "Í", "Ó", "Ú", "Ý", "à", "è", "ì", "ò", "ù", "À", "È", "Ì", "Ò", "Ù", "ã", "õ", "ñ", "ä", "ë", "ï", "ö", "ü", "ÿ", "Ä", "Ë", "Ï", "Ö", "Ü", "Ã", "Õ", "Ñ", "â", "ê", "î", "ô", "û", "Â", "Ê", "Î", "Ô", "Û" };
            var semAcento = new string[] { "c", "C", "a", "e", "i", "o", "u", "y", "A", "E", "I", "O", "U", "Y", "a", "e", "i", "o", "u", "A", "E", "I", "O", "U", "a", "o", "n", "a", "e", "i", "o", "u", "y", "A", "E", "I", "O", "U", "A", "O", "N", "a", "e", "i", "o", "u", "A", "E", "I", "O", "U" };

            for (var i = 0; i < acentos.Length; i++)
                str = str.Replace(acentos[i], semAcento[i]);

            string[] caracteresEspeciais = { "\\.", ",", "-", ":", "\\(", "\\)", "ª", "\\|", "\\\\", "°" };
            str = caracteresEspeciais.Aggregate(str, (current, t) => current.Replace(t, ""));
            str = str.Replace("^\\s+", "");
            str = str.Replace("\\s+$", "");
            str = str.Replace("\\s+", " ");

            return str;
        }
    }
}
